package com.openapi.handle;


import com.common.constant.ExceptionEnum;
import com.common.exception.CommonException;
import com.common.response.Response;
import com.common.response.ResponseUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**

 * REST接口的通用异常处理
 */
@ControllerAdvice
@ResponseBody
public class ExceptionHandle {
    private static final Logger logger = LoggerFactory.getLogger(LogHandle.class);

    /**
     * 业务异常处理
     * @param exception
     * @param <T>
     * @return
     */
    @ExceptionHandler(CommonException.class)
    public <T> Response<T> exceptionHandler(CommonException exception) {
        return ResponseUtils.returnException( exception);
    }

    /**
     * 请求方法不正确
     * @param exception
     * @return
     */
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    public Response HttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException exception) {
        throw new CommonException(ExceptionEnum.HTTP_REQ_METHOD_ERROR);
    }

    /**
     * 系统异常处理
     * @param exception
     * @return
     */
    @ExceptionHandler(Exception.class)
    public <T> Response<T> sysExpHandler(Exception exception) {
        logger.error("系统异常 ",exception);
        return ResponseUtils.returnException(exception);
    }
}
